import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
    /**
     * 生成1-100内数的方法
     */
    public int suiji() {
        Random random = new Random();
        int suiji = random.nextInt(100);
        return suiji;
    }

    /**
     * 判断猜测数据关系方法
     */
    public void panduan(int a) {
        if (a == 0)
            System.out.println("恭喜您答对了！！！");
        else if (a == 1)
            System.out.println("您猜的数字太大了");
        else if (a == 2)
            System.out.println("您猜的数字太小了");
    }

    /**
     * 判断玩家是否继续
     */
    public boolean jixu() {
        System.out.println("您是否继续游戏 Y/N");
        Scanner scanner = new Scanner(System.in);
        boolean panduan = true;
        while (true) {
            String shuru = scanner.next();
            if (shuru.equals("Y") || shuru.equals("y")) {
                panduan = true;
                break;
            } else if (shuru.equals("N") || shuru.equals("n")) {
                panduan = false;
                break;
            } else {
                System.out.println("输入错误重新输入");
            }
        }
        return panduan;
    }

    public static void main(String[] args) {
        GuessNumber guessNumber = new GuessNumber();
        //用户输入方法
        Scanner scanner = new Scanner(System.in);
        //提示用户输入信息
        System.out.println("猜数字游戏 ！！！");
        //创建变量控制答案关系
        int a = 0;
        //控制次数
        int cishu = 6;
        //控制游戏是否继续
        boolean jixu = true;
        int suiji = guessNumber.suiji();
        while (jixu) {
            while (cishu >= 0) {
                System.out.println("请您输入一个1-100之间的数字");
                //获取用户输入信息
                int shuru = scanner.nextInt();

                if (suiji == shuru) {
                    a = 0;
                    guessNumber.panduan(a);
                    break;
                } else if (shuru > suiji) {
                    a = 1;
                } else if (shuru < suiji)
                    a = 2;
                guessNumber.panduan(a);
                if (cishu > 0)
                    System.out.println("您还有" + cishu + "次机会");
                cishu--;
            }
            cishu = 6;
            jixu = guessNumber.jixu();
        }
    }
}
